import { createStore } from "redux";

const defState: IStoreState = {
  toastData: {
    isShow: false,
    message: "test",
    type: "error",
  },
};

// 这个reducer的state参数，需要给一个默认值，因为如果不给默认值，state可能是undefined
const reducer = (state: IStoreState = defState, action: IStoreAction) => {
  // 最好把state作一次深拷贝
  state = JSON.parse(JSON.stringify(state));
  switch (action.type) {
    case "Toast_Show":
      let payload = action.payload as IToastPayload;
      state.toastData.type = payload.type;
      state.toastData.message = payload.message;
      state.toastData.isShow = true;
      break;
    case "Toast_Hide":
      state.toastData.isShow = false;
      break;
  }
  return state;
};

const store = createStore(reducer);

export default store;
